package campus.aiit.ssl.manage.modbus;

import campus.aiit.ssl.manage.modbus.provider.Provider;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author ihaokun
 * @date 2019/12/17 22:19
 */
@Component
public class SendCommand {
  @Resource
  private SocketProcess socketProcess;

  /* 自控 */
  public boolean onAuto(SocketProcess.SDU sdu){
    composeWithProviderPdu(sdu, Provider.Constants.PDUs.ON_AUTO);
    return socketProcess.pushMessage(sdu);
  }
  public boolean offAuto(SocketProcess.SDU sdu){
    composeWithProviderPdu(sdu, Provider.Constants.PDUs.OFF_AUTO);
    return socketProcess.pushMessage(sdu);
  }
  public boolean autoStatus(SocketProcess.SDU sdu){
    composeWithProviderPdu(sdu, Provider.Constants.PDUs.AUTO_STATUS);
    return socketProcess.pushMessage(sdu);
  }
  public boolean onOrOffAuto(SocketProcess.SDU sdu){
    if (autoStatus(sdu)) return offAuto(sdu);
    else return onAuto(sdu);
  }
  /* 继电器 */
  public boolean relayStatus(SocketProcess.SDU sdu){
    composeWithProviderPdu(sdu, Provider.Constants.PDUs.RELAY_STATUS);
    return socketProcess.pushMessage(sdu);
  }
  public boolean onRelay(SocketProcess.SDU sdu){
    composeWithProviderPdu(sdu, Provider.Constants.PDUs.ON_RELAY);
    return socketProcess.pushMessage(sdu);
  }
  public boolean offRelay(SocketProcess.SDU sdu){
    composeWithProviderPdu(sdu, Provider.Constants.PDUs.OFF_RELAY);
    return socketProcess.pushMessage(sdu);
  }
  public boolean onOrOffRelay(SocketProcess.SDU sdu) {
    if (relayStatus(sdu)) return offRelay(sdu);
    else return onRelay(sdu);
  }

  public boolean onOffStatus(SocketProcess.SDU sdu){
    composeWithProviderPdu(sdu, Provider.Constants.PDUs.ON_OFF_STATUS);
    return socketProcess.pushMessage(sdu);
  }
  public int temperature(SocketProcess.SDU sdu){
    composeWithProviderPdu(sdu, Provider.Constants.PDUs.TEMPERATURE);
//    return socketProcess.pushMessage(sdu);
    return 0;
  }

  private void composeWithProviderPdu(SocketProcess.SDU sdu, Provider.Constants.PDUs providePdu){
    ModBus.ADU.PDU pdu = new ModBus.ADU.PDU(providePdu.toString());
    sdu.getAdu().setPdu(pdu);
  }
}